package com.hyt.it.ogt.opertaion.track.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyt.it.ogt.opertaion.track.config.OperationTrackMqProperties;
import com.hyt.it.ogt.opertaion.track.model.AnswerLogVO;
import com.hyt.it.ogt.opertaion.track.model.KsHandUpLogVO;
import com.hyt.it.ogt.opertaion.track.model.KsWarningLogVO;
import com.hyt.it.ogt.opertaion.track.model.OtherLogVO;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
@ConditionalOnClass({Channel.class, RabbitTemplate.class})
public class OperationTrackMqProducer {
    @Resource
    private OperationTrackMqProperties operationTrackMqProperties;
    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(Object obj) {
        if (obj instanceof List) {
            sendMessageOnList(obj);
        } else if (obj instanceof AnswerLogVO) {
            ksAnswerSendMessage(obj);
        } else if (obj instanceof OtherLogVO) {
            ksOtherSendMessage(obj);
        } else if (obj instanceof KsWarningLogVO) {
            kwWarnSendMessage(obj);
        } else
            throw new IllegalStateException("没有匹配推送对象:" + obj.getClass() + ",推送数据:" + JSONObject.toJSONString(obj));
    }

    public void sendMessageOnList(Object obj) {
        List<?> data = (List<?>) obj;
        if (data.isEmpty()) {
            log.error("OperationTrackMqProducer推送数据为空");
            return;
        }
        List<AnswerLogVO> answerLogVOList = new ArrayList<>();
        List<OtherLogVO> otherLogVOList = new ArrayList<>();
        List<KsWarningLogVO> ksWarningLogVOList = new ArrayList<>();
        List<KsHandUpLogVO> ksHandUpLogVOList = new ArrayList<>();
        data.forEach(vo -> {
            if (vo instanceof AnswerLogVO) {
                answerLogVOList.add((AnswerLogVO) vo);
            } else if (vo instanceof OtherLogVO) {
                otherLogVOList.add((OtherLogVO) vo);
            } else if (vo instanceof KsWarningLogVO) {
                ksWarningLogVOList.add((KsWarningLogVO) vo);
            } else if (vo instanceof KsHandUpLogVO) {
                ksHandUpLogVOList.add((KsHandUpLogVO) vo);
            }
        });
        if (!ksWarningLogVOList.isEmpty()) {
            kwWarnSendMessage(ksWarningLogVOList);
        }
        if (!ksHandUpLogVOList.isEmpty()) {
            kwHandUpSendMessage(ksHandUpLogVOList);
        }
        if (!answerLogVOList.isEmpty()) {
            ksAnswerSendMessage(answerLogVOList);
        }
        if (!otherLogVOList.isEmpty()) {
            ksOtherSendMessage(otherLogVOList);
        }
    }

    public void kwWarnSendMessage(Object obj) {
        rabbitTemplate.convertAndSend(operationTrackMqProperties.getKwOperationTrackWarnExchange(),
                operationTrackMqProperties.getKwOperationTrackWarnRoute(), JSON.toJSONString(obj),
                message -> message);
        /*if (log.isDebugEnabled()) {
            log.debug("kwWarnSendMessage send json:{}", obj.toString());
        }*/
    }

    public void kwHandUpSendMessage(Object obj) {
        rabbitTemplate.convertAndSend(operationTrackMqProperties.getKwOperationTrackHandUpExchange(),
                operationTrackMqProperties.getKwOperationTrackHandUpRoute(), JSON.toJSONString(obj),
                message -> message);
        /*if (log.isDebugEnabled()) {
            log.debug("kwHandUpSendMessage send json:{}", obj.toString());
        }*/
    }

    public void ksAnswerSendMessage(Object obj) {
        rabbitTemplate.convertAndSend(operationTrackMqProperties.getKsOperationTrackAnswerExchange(),
                operationTrackMqProperties.getKsOperationTrackAnswerRoute(), JSON.toJSONString(obj),
                message -> message);
        /*if (log.isDebugEnabled()) {
            log.debug("ksAnswerSendMessage send json:{}", obj.toString());
        }*/
    }

    public void ksOtherSendMessage(Object obj) {
        rabbitTemplate.convertAndSend(operationTrackMqProperties.getKsOperationTrackOtherExchange(),
                operationTrackMqProperties.getKsOperationTrackOtherRoute(), JSON.toJSONString(obj),
                message -> message);
        /*if (log.isDebugEnabled()) {
            log.debug("ksOtherSendMessage send json:{}", obj.toString());
        }*/
    }
}

